// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using Microsoft.WindowsMobile.Status;
using svs.Mobile.BeyondScheduler.Core.Objects;
using svs.Mobile.BeyondScheduler.Core.Objects.Operations;

namespace svs.Mobile.BeyondScheduler.UI
{
	public partial class TaskProperties : Form
	{
		private OperationBase _CurrentOperation;
		private ScheduledTask _CurrentTask;

		public TaskProperties()
		{
			InitializeComponent();
		}

		private void OnOperationsChanged(object sender, EventArgs e)
		{
			 OperationBase selected = cmbOperations.SelectedItem as OperationBase;

			//Make sure we are working with the correct instance - the one we are editing
			 if (_CurrentTask.Operation != null && _CurrentTask.Operation.ID.Equals(selected.ID))
				 _CurrentOperation = _CurrentTask.Operation;
			 else
			 	_CurrentOperation = selected;

			 lblConfigureOperation.Enabled = _CurrentOperation.IsConfigurable;

		}

		public void SetCurrentAction(ScheduledTask task)
		{
			_CurrentTask = task;
		}

		private void OnLoad(object sender, EventArgs e)
		{
			cmbOperations.DisplayMember = "Description";
			cmbOperations.DataSource = TaskRepository.OperationsForAssignment;

			KeyValuePair<string, TimeSpan>[] periods = {
			                                           	new KeyValuePair<string, TimeSpan>("Hours", new TimeSpan(1, 0, 0)),
			                                           	new KeyValuePair<string, TimeSpan>("Days", new TimeSpan(1, 0, 0, 0)),
			                                           	new KeyValuePair<string, TimeSpan>("Weeks", new TimeSpan(7, 0, 0, 0))
			                                           };


			KeyValuePair<string, BatteryLevel>[] battLevels = {
			                                                  	new KeyValuePair<string, BatteryLevel>("Any Level",
			                                                  	                                       BatteryLevel.VeryLow),
			                                                  	new KeyValuePair<string, BatteryLevel>("At Least 80%",
			                                                  	                                       BatteryLevel.VeryHigh),
			                                                  	new KeyValuePair<string, BatteryLevel>("At Least 60%",
			                                                  	                                       BatteryLevel.High),
			                                                  	new KeyValuePair<string, BatteryLevel>("At Least 40%",
			                                                  	                                       BatteryLevel.Medium),
			                                                  	new KeyValuePair<string, BatteryLevel>("At Least 20%",
			                                                  	                                       BatteryLevel.Low)
			                                                  };


			cmbBatteryLevel.DisplayMember = "Key";
			cmbBatteryLevel.ValueMember = "Value";
			cmbBatteryLevel.DataSource = battLevels;

			cmbRecurrence.DisplayMember = "Key";
			cmbRecurrence.ValueMember = "Value";
			cmbRecurrence.DataSource = periods;

			if (_CurrentTask == null)
				return;


			_CurrentOperation = _CurrentTask.Operation;
			SelectCurrentOperation();

			ctrlStartTime.Value = _CurrentTask.Event.StartTime ?? DateTime.Now;
			ctrlStartTime.Value.AddSeconds(ctrlStartTime.Value.Second * -1); // clear the seconds

			cmbRecurrence.SelectedValue = _CurrentTask.Event.RecurrencePeriod;
			txtInterval.Text = _CurrentTask.Event.RecurrenceInterval.ToString();

			lblConfigureOperation.Enabled = _CurrentTask.Operation.IsConfigurable;

			cmbBatteryLevel.SelectedValue = _CurrentTask.MinBatteryLevelRequired;

			ctrlStartTime.Focus();

#if  Smartphone
			InputModeEditor.SetInputMode(txtInterval, InputMode.Numeric);
#endif

		}

		private void SelectCurrentOperation()
		{
			if (_CurrentOperation == null && cmbOperations.Items.Count > 0)
				cmbOperations.SelectedIndex = 0;

			foreach (OperationBase operationBase in cmbOperations.Items)
			{
				if (operationBase.ID.Equals(_CurrentOperation.ID))
					cmbOperations.SelectedItem = operationBase;
			}
		}

		private void menuCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void menuDone_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
			Close();
		}

		private void OnConfigureOperationClick(object sender, EventArgs e)
		{
			_CurrentOperation.ShowConfigurationUI(this);
		}

		private bool CheckAndSaveSettings()
		{
			if (_CurrentOperation is OperationNull)
			{
				MessageBox.Show("Please select a Task to execute!", "Invalid Task", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
												MessageBoxDefaultButton.Button1);
				return false;
			}

			int interval;

			try
			{
				interval = int.Parse(txtInterval.Text.Trim());
			}
			catch
			{
				MessageBox.Show("Please select a valid recurrence interval!", "Invalid Interval", MessageBoxButtons.OK,
												MessageBoxIcon.Exclamation,
												MessageBoxDefaultButton.Button1);
				txtInterval.Focus();
				return false;
			}

			_CurrentTask.Event.RecurrencePeriod = (TimeSpan)cmbRecurrence.SelectedValue;
			_CurrentTask.Event.RecurrenceInterval = interval;

			_CurrentTask.Event.UnadjustedStartTime = ctrlStartTime.Value;

			_CurrentTask.Operation = _CurrentOperation;
			_CurrentTask.MinBatteryLevelRequired = (BatteryLevel)cmbBatteryLevel.SelectedValue;

			return true;
		}

		private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				e.Cancel = (CheckAndSaveSettings() == false);
			}
		}
	}
}